﻿using System;
using System.Data;
using System.Web.UI.WebControls;

using BLL;
using Model;
using Utility;

namespace EvaluationSystem
{
    public partial class TestEnd : System.Web.UI.Page
    {
        private QuestionStatusBLL m_QuestionStatusBll = new QuestionStatusBLL();
        private TesterInfoBLL m_TesterInfoBll = new TesterInfoBLL();
        private DimensionBLL m_DimensionBll = new DimensionBLL();
        private GuidBLL m_GuidBll = new GuidBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["isTesting"] == null)//如果没有输入序列号，则禁止访问此页面
            {
                Response.Redirect("Tlogin.aspx");
            }

            Response.Write("<div id='mydiv'>");
            Response.Write("_");
            Response.Write("</div>");
            Response.Write("<script>mydiv.innerText = '';</script>");
            Response.Write("<script language=javascript>;");
            Response.Write("var dots = 0;var dotmax = 10;function ShowWait()");
            Response.Write("{var output; output = '正在提交答题结果';dots++;if(dots>=dotmax)dots=1;");
            Response.Write("for(var x = 0;x < dots;x++){output += '·';}mydiv.innerText =  output;}");
            Response.Write("function StartShowWait(){mydiv.style.visibility = 'visible'; ");
            Response.Write("window.setInterval('ShowWait()',1000);}");
            Response.Write("function HideWait(){mydiv.style.visibility = 'hidden';");
            Response.Write("window.clearInterval();}");
            Response.Write("StartShowWait();</script>");
            Response.Flush();
            System.Threading.Thread.Sleep(4000);

            if (!IsPostBack)
            {
                if (Request["guid"] != null)
                {
                    m_TesterInfoBll.UpdateTesterInfo(Request["guid"].ToString(), "tester_testendtime", DateTime.Now.ToString());

                    //记录答题结果
                    DataTable dtTmp = (DataTable)CacheManager.Get("usrSelected_" + Request["guid"].ToString());
                    foreach (DataRow row in dtTmp.Rows)
                    {
                        string test_sn = row["test_sn"].ToString();
                        int questionID = Int32.Parse(row["question_id"].ToString());
                        string question_sel = row["question_selected"].ToString();
                        int question_is_dirty = Int32.Parse(row["question_is_dirty"].ToString());
                        DateTime dt_start = (DateTime.Parse(row["question_start_dt"].ToString()));
                        DateTime dt_end = (DateTime.Parse(row["question_end_dt"].ToString()));

                        m_QuestionStatusBll.RecordScore(test_sn,
                                                        questionID,
                                                        question_sel,
                                                        question_is_dirty,
                                                        dt_start,
                                                        dt_end);
                    }

                    //计算总分并更新到TesterInfo表中
                    RptBLL rptBLL = new RptBLL();
                    double score = rptBLL.GetTotalSocre(Request["guid"].ToString());
                    m_TesterInfoBll.UpdateTesterInfo(Request["guid"].ToString(), "tester_matching", score.ToString());

                    //考察本次测评结果是否可信
                    if (true == rptBLL.IsTrust(Request["guid"].ToString()))
                    {
                        //可信
                        m_TesterInfoBll.UpdateTesterInfo(Request["guid"].ToString(), "tester_consistencyscore", "1");
                    }
                    else
                    {
                        //不可信
                        m_TesterInfoBll.UpdateTesterInfo(Request["guid"].ToString(), "tester_consistencyscore", "0");
                    }

                    //测试完成
                    m_GuidBll.UpdateStatus(Request["guid"].ToString(), 2);
                    CacheManager.Clear(Request["guid"].ToString());//清空缓存

                    Session["isTesting"] = null;

                    Response.Write("<script language='javascript'>alert('恭喜，此次答题完成，系统已经记录您的成绩');</script>");
                    /*
                    Response.Write("<script>");
                    Response.Write("{top.opener =null;top.close();}");
                    Response.Write("</script>");
                     */
                    //Response.Write("<script language='javascript'>window.parent.close())</script>");

                    ActivityBLL activityBll = new ActivityBLL();
                    Activity activity = activityBll.GetModel(m_GuidBll.GetActivityId(Request.QueryString["guid"]));
                    int nPartFlag = activity.Activity_viewreport;

                    //如果企业端允许查看 报告，则测评完毕后自动转入查看报告页面。
                    if (nPartFlag.ToString().Length != 7)
                    {
                        Response.Write("<script language='javascript'>window.navigate(\"Tlogin.aspx\")</script>");
                    }
                    else //企业端允许
                    {
                        if (nPartFlag.ToString().Substring(0, 1).Equals("1"))
                        {
                            Response.Write("<script language='javascript'>alert('您已完成答题，点击“确定”可现在查看报告。若需以后查看,请记录下该网址:answer.kingmas.com.cn,凭本次序列号查看。');window.navigate(\"../Report/GenPdfReport.aspx?sn=" + Request.QueryString["guid"] + "\")</script>");
                        }
                        else
                        {
                            Response.Write("<script language='javascript'>window.navigate(\"Tlogin.aspx\")</script>");
                        }
                    }
                }
            }
        }
    }
}